package com.cdtu.service;

import com.cdtu.mapper.EmpMapper;
import com.cdtu.pojo.Emp;
import com.cdtu.pojo.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService{
    @Autowired
    private EmpMapper empMapper;

//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
    //获取总记录数
//        Long count = empMapper.count();

//        List<Emp> empList = empMapper.page((page - 1) * pageSize, pageSize);
//        PageBean pageBean = new PageBean(count, empList);//-----------------------------------first
//        return pageBean;
//    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //设置分页参数
        PageHelper.startPage(page, pageSize);
        //获取员工信息集合
        List<Emp> empList = empMapper.list (name,gender,begin,end);
        //集合转成page,获取分页结果
        Page<Emp> p = (Page<Emp>) empList;
        //构造pagebean
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }
/*
* 批量删除操作
* */
    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    /*
    * 添加员工
    * */
    @Override
    public void add(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }
/*
* 根据ID查询
* */
    @Override
    public Emp getById(Integer id) {
        Emp emp = empMapper.select(id);
        return emp;
    }
/*
* 修改
* */
    @Override
    public void updateById(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }


    @Override
    public Emp login(Emp emp) {
        Emp e = empMapper.getByUsernameAndPassword(emp);
        return e;
    }
}
